package com.tarena.lbs.activity.service.impl;

import com.tarena.lbs.activity.mapper.ActivityMapper;
import com.tarena.lbs.activity.pojo.dto.ActivityAddParam;
import com.tarena.lbs.activity.pojo.dto.ActivityUpdateParam;
import com.tarena.lbs.activity.pojo.entity.ActivityE;
import com.tarena.lbs.activity.pojo.vo.ActivityQuery;
import com.tarena.lbs.activity.pojo.vo.ActivityVO;
import com.tarena.lbs.activity.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    ActivityMapper activityMapper;

    @Override
    public void addActivity(ActivityAddParam activityAddParam) {
        log.debug("保存添加的活动：{}",  activityAddParam);
        ActivityE activityE = new ActivityE();
        BeanUtils.copyProperties(activityAddParam, activityE);
//        Date StartDate = activityAddParam.setStartDate();
        Date now = new Date();
        activityE.setCreateAt(now);
        activityMapper.insert(activityE);
    }

    @Override
    public void updateActivity(Long id, ActivityUpdateParam activityUpdateParam) {

        log.debug("修改活动：{},内容：{}", id,activityUpdateParam);
        ActivityE activityE = activityMapper.selectById(id);
        if (activityE == null) {
            throw new RuntimeException("活动不存在，ID：" + id);
        }
        BeanUtils.copyProperties(activityUpdateParam, activityE);
        activityE.setId(id);
        activityE.setUpdateAt(new Date());

        activityMapper.update(activityE);
    }

    @Override
    public void deleteActivity(Long id) {
        log.debug("删除活动：{}", id);
        ActivityE activityE = activityMapper.selectById(id);
        if (activityE == null) {
            throw new RuntimeException("活动不存在，ID：" + id);
        }else {
            activityMapper.delete(id);
        }


    }

    @Override
    public List<ActivityVO> selectActivity(ActivityQuery activityQuery) {
        log.debug("查询活动：{}", activityQuery);
        List<ActivityVO> activityVOList = activityMapper.selectActivity(activityQuery);

        return activityVOList;
    }


}
